// Stolen from the Scribefire code as getting spellcheck to work is ridiculously tedious and they've figured it out already.
//****************************************************
// Handles all of the spellchecking in scribefire.
//
// TODO: Make it spellcheck text that has been loaded
//       from last time right away, instead of
//       waiting for it to change first.
//****************************************************

function dsSpellCheck() {
	this.prefs = null;
}

//init the dsSpellCheck object
dsSpellCheck.prototype.init = function(theTextBox) {
	//load the javascript loader
	var loader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"].getService(Components.interfaces.mozIJSSubScriptLoader);
        //use the above javascript loader to load the provided spellcheck wrapper
	loader.loadSubScript("chrome://global/content/inlineSpellCheckUI.js", this);
	//set up the provided wrapper with the text object
	this.InlineSpellCheckerUI.init(theTextBox);                                                        
	//turn on the provided wrapper
	this.InlineSpellCheckerUI.enabled = true;
}

dsSpellCheck.prototype.isFX3OrGreater = function() {
      var appInfo = Components.classes["@mozilla.org/xre/app-info;1"].getService(Components.interfaces.nsIXULAppInfo);
      var versionChecker = Components.classes["@mozilla.org/xpcom/version-comparator;1"].getService(Components.interfaces.nsIVersionComparator);
      if (versionChecker.compare(appInfo.version, "3.0b5") >= 0) {
        this.isFx3orGreater = true;
        return true;
      }else{
          this.isFx3orGreater = false;
	  if ((appInfo.ID == "{92650c4d-4b8e-4d2a-b7eb-24ecf4f6b63a}") && ("@mozilla.org/spellchecker;1" in Components.classes)) {
		  document.getElementById("cmd_spell").removeAttribute("disabled");
	  }
      }
      return false;
}

//generates and handles the context menu items (spelling suggestions, etc.)
//popupNode is the context menu with which to work
dsSpellCheck.prototype.initContextMenu = function(popupNode) {
	//if the spellcheck wrapper says it can't run, hide the context items and leave
	if (!this.InlineSpellCheckerUI.canSpellCheck) {
		this.hideContextSpellingItems();
		return;
	}
        //pass the context menu to the wrapper
        this.InlineSpellCheckerUI.initFromEvent(document.popupRangeParent, document.popupRangeOffset);
        
        //set the context-menu item for toggling spell-check to the enabled var from the wrapper
        document.getElementById("spell-check-enabled").setAttribute("checked", this.InlineSpellCheckerUI.enabled);

        //set var wordMisspelled to the wrapper's var
        //this var is true if the context menu was raised when the mouse was over a misspelled word
        var wordMisspelled = this.InlineSpellCheckerUI.overMisspelling;
        
        //toggle the spelling suggestions and the add-to-dictionary items depending on overMisspelling
        this.setElementVisibility("spell-add-to-dictionary", wordMisspelled);
        this.setElementVisibility("spell-suggestions-separator", wordMisspelled);

        //set the number of suggestions
        var numsug = this.InlineSpellCheckerUI.addSuggestionsToMenu(popupNode, document.getElementById("spell-suggestions-separator"), 5);

        //set the visibility of the nosuggestions item
        this.setElementVisibility("spell-no-suggestions", (wordMisspelled && numsug == 0));

        //set the number of dictionaries
        var numdicts = this.InlineSpellCheckerUI.addDictionaryListToMenu(document.getElementById("spell-dictionaries-menu"), null);
        
        //set the visibility of the dictionary-changer as long as there are more than one dictionary to change between
        this.setElementVisibility("spell-dictionaries", (this.InlineSpellCheckerUI.enabled && numdicts > 1));
}

//clears the dictionaries and spelling suggestions from the context menu
dsSpellCheck.prototype.clearContextMenu = function(popupNode)
{
        if (this.InlineSpellCheckerUI)
        {
                this.InlineSpellCheckerUI.clearSuggestionsFromMenu();
                this.InlineSpellCheckerUI.clearDictionaryListFromMenu();
        }
}

//sets the visibility of an element by elementID
dsSpellCheck.prototype.setElementVisibility = function(elementID, visible)
{
        try
        {
                document.getElementById(elementID).hidden = !visible;
        } catch(e){}
}

//hides the spelling-related context-menu items
dsSpellCheck.prototype.hideContextSpellingItems = function()
{
        this.setElementVisibility("spell-no-suggestions", false);
        this.setElementVisibility("spell-check-enabled", false);
        this.setElementVisibility("spell-check-separator", false);
        this.setElementVisibility("spell-add-to-dictionary", false);
        this.setElementVisibility("spell-suggestions-separator", false);
        this.setElementVisibility("spell-dictionaries", false);
}

dsSpellCheck.prototype.addToDictionary = function()
{
        try
        {
                this.InlineSpellCheckerUI.addToDictionary();
        } catch(e){}
}

dsSpellCheck.prototype.toggleEnabled = function()
{
        try
        {
                this.InlineSpellCheckerUI.toggleEnabled();
        } catch(e){}
}

//handles the web-page opening for adding dictionaries depending on the version
dsSpellCheck.prototype.addDictionaries = function()
{
        //set the uri value as a url
        var formatter = Components.classes["@mozilla.org/toolkit/URLFormatterService;1"].getService(Components.interfaces.nsIURLFormatter);
        var uri = formatter.formatURLPref("browser.dictionaries.download.url");

        //set the locale as not null, then try and get it from Firefox
        var locale = "-";
        try
        {
                locale = this.prefs.getComplexValue("intl.accept_languages", Components.interfaces.nsIPrefLocalizedString).data;
        } catch(e){}

        //set the version as not null, then try and get it from Firefox
        var version = "-";
        try
        {
                version = Components.classes["@mozilla.org/xre/app-info;1"].getService(Components.interfaces.nsIXULAppInfo).version;
        } catch(e){ }

        //add the proper locale and version to the uri
        uri = uri.replace(/%LOCALE%/, escape(locale));
        uri = uri.replace(/%VERSION%/, version);

        //prepare to open it in a new tab
        var newWindowPref = this.prefs.getIntPref("browser.link.open_newwindow");
        var where = newWindowPref == 3 ? "tab" : "window";
        
        window.top.gBrowser.selectedTab = window.top.gBrowser.addTab(uri);
}

function suiteSpellCheck() {
	// Quick hack-job so it'll work in SM with a minimal amount of code changing through the proggie.
	 var appInfo = Components.classes["@mozilla.org/xre/app-info;1"].getService(Components.interfaces.nsIXULAppInfo);
	 if ((appInfo.ID == "{92650c4d-4b8e-4d2a-b7eb-24ecf4f6b63a}") && ("@mozilla.org/spellchecker;1" in Components.classes))
	window.openDialog("chrome://editor/content/EdSpellCheck.xul", "_blank","chrome,close,titlebar,modal", false, false, true);
}
